package com.qeegoo.b2bautozityp.moudle.good.viewmodel.category;

import com.chad.library.adapter.base.entity.MultiItemEntity;
import com.kelin.mvvmlight.command.ReplyCommand;
import com.qeegoo.b2bautozityp.api.HttpParams;
import com.qeegoo.b2bautozityp.api.ProgressSubscriber;
import com.qeegoo.b2bautozityp.api.RequestApi;
import com.qeegoo.b2bautozityp.api.RetrofitService;
import com.qeegoo.b2bautozityp.moudle.good.adapter.ExpandableItemAdapter;
import com.qeegoo.b2bautozityp.moudle.good.entity.Level0Item;
import com.qeegoo.b2bautozityp.moudle.good.model.GoodsCategoryBean;
import com.qeegoo.b2bautozityp.moudle.good.view.category.GoodsCategorySearchActivity;

import java.util.ArrayList;

import base.lib.util.ActivityManager;
import base.lib.util.NavigateUtils;

/**
 * 王少岩 在 2017/9/19 创建了它
 */

public class GoodsCategoryViewModel {
    private RequestApi mRequestApi;
    private ExpandableItemAdapter mAdapter;
    private ArrayList<MultiItemEntity> mEntities = new ArrayList<>();

    public GoodsCategoryViewModel(RequestApi requestApi) {
        mRequestApi = requestApi;
        mAdapter = new ExpandableItemAdapter(mEntities, mRequestApi);
        getCategorys();
    }

    public ExpandableItemAdapter getAdapter() {
        return mAdapter;
    }

    public void getCategorys() {
        mRequestApi.listProductByLevel(HttpParams.goodsCategory(""))
                .compose(RetrofitService.applySchedulers())
                .map(new RetrofitService.HttpResultFunc<>())
                .subscribe(new ProgressSubscriber<ArrayList<GoodsCategoryBean>>() {
                    @Override
                    public void onNext(ArrayList<GoodsCategoryBean> goodsCategorys) {
                        mEntities.clear();
                        for (GoodsCategoryBean bean : goodsCategorys) {
                            mEntities.add(new Level0Item(bean));
                        }
                        mAdapter.notifyDataSetChanged();
                    }
                });
    }

    public ReplyCommand leftCommand = new ReplyCommand(() -> ActivityManager.getActivity().finish());
    public ReplyCommand searchCommand = new ReplyCommand(() -> NavigateUtils.startActivityForResult(GoodsCategorySearchActivity.class, 1000));
}
